<?php
require_once(dirname(__FILE__) . '/include/config.inc.php');
$url = "http://www.mysd168.com/api/author/category";

function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;
}


$res = '{"data":{"authorcategory":[{"categoryid":4,"name":"\u7814\u9ad8\/\u5927\u5e08\u5178\u85cf\u81fb\u54c1","author":[{"authorid":27,"zname":"\u674e\u660c\u9e3f"},{"authorid":28,"zname":"\u9ec4\u81ea\u82f1"},{"authorid":29,"zname":"\u80e1\u6d2a\u660e"},{"authorid":30,"zname":"\u5b59\u4f2f\u6625"},{"authorid":31,"zname":"\u5f90\u5b89\u78a7"},{"authorid":32,"zname":"\u7a0b\u8f89"},{"authorid":33,"zname":"\u848b\u5f66"},{"authorid":34,"zname":"\u987e\u7ecd\u57f9"},{"authorid":35,"zname":"\u675f\u65e6\u751f"},{"authorid":36,"zname":"\u6c64\u9e23\u768b"},{"authorid":45,"zname":"\u6bdb\u56fd\u5f3a"}]},{"categoryid":10,"name":"\u9ad8\u5de5\/\u85cf\u5bb6\u5fc5\u5907","author":[{"authorid":4,"zname":"\u8d3e\u76ca\u82b3"},{"authorid":13,"zname":"\u9ec4\u65ed\u5cf0"},{"authorid":14,"zname":"\u8303\u4e91\u677e"},{"authorid":15,"zname":"\u9648\u5b8f\u6797"},{"authorid":16,"zname":"\u9c8d\u9752"},{"authorid":19,"zname":"\u7533\u5c60\u56fd\u6d2a"},{"authorid":20,"zname":"\u5f20\u5f69\u82f1"},{"authorid":21,"zname":"\u6c6a\u53f6"},{"authorid":22,"zname":"\u848b\u5cf0"},{"authorid":23,"zname":"\u6768\u7476\u82ac"}]},{"categoryid":11,"name":"\u5de5\u827a\u5e08\/\u7ecf\u5178\u5347\u503c","author":[{"authorid":24,"zname":"\u8303\u80b2\u541b"},{"authorid":25,"zname":"\u987e\u8dc3\u98de"},{"authorid":26,"zname":"\u5468\u56fd\u65b0"},{"authorid":41,"zname":"\u5e84\u6c38\u5fe0"},{"authorid":42,"zname":"\u5434\u5c0f\u7434"},{"authorid":43,"zname":"\u848b\u9759\u660e"},{"authorid":44,"zname":"\u7a46\u660e\u9f99"},{"authorid":55,"zname":"\u5f3a\u56fd\u9752"}]},{"categoryid":12,"name":"\u52a9\u5de5\/\u6f5c\u529b\u65b0\u79c0","author":[{"authorid":37,"zname":"\u94b1\u9676\u5cf0"},{"authorid":38,"zname":"\u5434\u7965\u534e"},{"authorid":39,"zname":"\u4f59\u7f8e\u73cd"},{"authorid":40,"zname":"\u90c1\u6c38\u826f"},{"authorid":52,"zname":"\u5f20\u9759\u73cd"}]},{"categoryid":13,"name":"\u7f8e\u672f\u5458\/\u6c11\u95f4\u9ad8\u624b","author":[{"authorid":46,"zname":"\u5468\u7acb\u5cf0"},{"authorid":47,"zname":"\u65b9\u660e"},{"authorid":48,"zname":"\u6bb5\u4e3d\u5a1f"},{"authorid":49,"zname":"\u8303\u7389\u840d"},{"authorid":50,"zname":"\u8bb8\u6653\u4e91"},{"authorid":51,"zname":"\u738b\u534e\u82b3"}]}]},"error":"","status":1}';


$arr = json_decode($res, true);

$arr = $arr['data']['authorcategory'];
foreach ($arr as $key => $val) {

    echo $key;
    var_dump($val);


    $dddd=$val['author'];
    arsort($dddd);



    foreach ($dddd as $k => $v) {


        $title = $v['zname'];
        $tbid=$v['authorid'];
        $content = "";
        $time = time();

        $channel_id = $key + 2;

        $dosql->Execute("insert into `fa_cms_archives`(channel_id,model_id,title,image,createtime,updatetime,publishtime,status,tbid) values ('$channel_id','2','$title','','$time','$time','$time','normal','$tbid')");
        $lastid = $dosql->GetLastID();
        $dosql->ExecNoneQuery("insert into `fa_cms_addonmingjia`(`id`,`content`) values ('$lastid','$content')");


        echo $title . "添加成功" . "<br>";
    }

}


/*
 * 获取一个远程图片
 *
 * @access  public
 * @param   $url       string  获取字段所属模型
 * @return  $save_url  string  返回上传后地址
*/
function GetRemPic($url)
{

    $filename_arr = parse_url($url);
    $path = $filename_arr['path'];
    $filename_arr = explode('/', $path);
    $filename = array_pop($filename_arr);

    $upload_url = '/uploads/image/news/';
    $upload_dir = PHPMYWIND_UPLOAD . "/images/news/";

    //上传文件路径
    $save_url = $upload_url . $filename;
    $save_dir = $upload_dir . $filename;
    echo "保存路径" . $save_dir . "<br/>";

    if (file_exists($save_dir)) {
        return $save_url;
    }


    //引入下载类
    require_once(PHPMYWIND_DATA . '/httpfile/down.class.php');

    //初始化变量
    $htd = new HttpDown();
    $htd->OpenUrl($url);


    //判断文件类型
    $sparr = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/xpng', 'image/wbmp');
    if (!in_array($htd->GetHead("content-type"), $sparr)) {
        return FALSE;
    } else {
        $type = $htd->GetHead("content-type");

        if ($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if ($type == 'image/png')
            $tempfile_ext = 'png';

        else if ($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        echo "保存";

        $rs = $htd->SaveToBin($save_dir);
    }

    $htd->Close();
    return ($rs ? $save_url : '');
}


?>